/**
 * 
 */
package org.gov.vis.data.gui.listener;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;

import javax.swing.JOptionPane;

import org.gov.vis.data.controller.ProgencyCtlr;
import org.gov.vis.data.controller.ProgencyCtlrImpl;
import org.gov.vis.data.dto.FollowUpDetailsDto;
import org.gov.vis.data.gui.EditProgencyDialog;
import org.gov.vis.data.gui.EditUpdateFollowUpDetailsDialog;
import org.gov.vis.data.gui.ViewProcurementSemenDialog;
import org.gov.vis.main.Main;
import org.gov.vis.master.gui.listener.BaseListener;
import org.gov.vis.util.DBConstants;
import org.gov.vis.util.DU;
import org.gov.vis.util.GuiConstants;
import org.gov.vis.util.Util;
import org.gov.vis.util.ValidatorUtils;

/**
 * @author NAGESH
 *
 */
public class EditUpdateFollowUpDetailsListener implements ActionListener,BaseListener{

	final private ResourceBundle rb = Util.getResources();
	List<FollowUpDetailsDto> totalLst;
	private EditUpdateFollowUpDetailsDialog followUpDetailsDialog;
	private FollowUpDetailsDto followUpDetailsDto;
	public HashMap<String, String> errorMsgs = new HashMap<String, String>();

	//public static List<FollowUpDetailsDto> tempFollowUpLst = new ArrayList<FollowUpDetailsDto>();
	private ProgencyCtlr progencyCtlr = new ProgencyCtlrImpl();
	List<FollowUpDetailsDto> followUpLst = EditProgencyDialog.tempFollowUpLst;
	private long _followUpId = -1;
	EditProgencyDialog editProgencyDialog;
	private boolean errorsFound = false;

	public EditUpdateFollowUpDetailsListener(EditUpdateFollowUpDetailsDialog followUpDetailsDialog){

		this.followUpDetailsDialog = followUpDetailsDialog;
		followUpLst = new ArrayList<FollowUpDetailsDto>();
	}


	@Override
	public void actionPerformed(ActionEvent actionEvent) {
		// TODO Auto-generated method stub

		if(actionEvent.getActionCommand().equals("Update")){
			errorsFound = false;
			followUpDetailsDto = (FollowUpDetailsDto)followUpDetailsDialog.getFollowUpDetailsObject();
			//totalLst = followUpDetailsDialog.getTmpFollowUpLst();
			if(followUpDetailsDto != null){
				if(followUpDetailsDto.getFollowUpId() > 0 ){
					_followUpId = followUpDetailsDto.getFollowUpId();
				}
				
				//if(followUpDetailsDialog.getTmpFollowUpLst() != null)
				followUpDetailsDialog.getTmpFollowUpLst().remove(followUpDetailsDto);
			}
			addFollowUpDetails();
			if(!errorsFound){
				followUpLst.add(followUpDetailsDto);
				totalLst = followUpDetailsDialog.getTmpFollowUpLst();
				totalLst.addAll(followUpLst);
				followUpDetailsDialog.setTmpFollowUpLst(totalLst);
				followUpDetailsDialog.dispose();
				editProgencyDialog = (EditProgencyDialog)followUpDetailsDialog.getProgencyObject();
				//EditProgencyDialog editProgencyDialog = (EditProgencyDialog)followUpDetailsDialog.getProgencyObject();
				EditProgencyDialog.tempFollowUpLst = totalLst;
				editProgencyDialog.refreshScreen(totalLst);
			}
		}
		if(actionEvent.getActionCommand().equals("Delete")){
			followUpDetailsDto = (FollowUpDetailsDto)followUpDetailsDialog.getFollowUpDetailsObject();
			editProgencyDialog = (EditProgencyDialog)followUpDetailsDialog.getProgencyObject();
			int response = JOptionPane.showConfirmDialog(null, GuiConstants.DELETE_CONFIRM_MSG, "Confirm",
					JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
			if (response == JOptionPane.YES_OPTION) {	
				try {
					if(followUpDetailsDto.getFollowUpId() > 0){
						progencyCtlr.deleteFollowUpProgency(followUpDetailsDto.getFollowUpId());
					}
					JOptionPane.showMessageDialog(editProgencyDialog, rb.getString(GuiConstants.DATA_DELETE_PROCUREMENTSEMEN_SUCCESS));
					followUpDetailsDialog.getTmpFollowUpLst().remove(followUpDetailsDto);
					totalLst = followUpDetailsDialog.getTmpFollowUpLst();
					followUpDetailsDialog.dispose();
					EditProgencyDialog.tempFollowUpLst = totalLst;
					editProgencyDialog.refreshScreen(totalLst);
				} catch (Exception e) {
					List<String> errorMsgLst = new ArrayList<String>();
					errorMsgLst.add(e.getMessage());
					//errorMsgPanel.setErrorMessages(errorMsgLst);
				}
			} else if (response == JOptionPane.NO_OPTION || response == JOptionPane.CLOSED_OPTION) {
				return;
			}
		}



	}

	private void addFollowUpDetails() {
		// TODO Auto-generated method stub
		setGuiDetailsToDto(followUpDetailsDialog);

		//need to perform validations and save the FollowUpDetails
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		followUpDetailsDialog.getTxtBxBirthWeight().setText("");
		followUpDetailsDialog.getDtChoDateOfDeticking().setDate(null);
		followUpDetailsDialog.getDtChoDateOfDeworming().setDate(null);
		followUpDetailsDialog.getDtChoDateOfVaccination().setDate(null);
		followUpDetailsDialog.getTxtBxNutrition().setText(null);
		followUpDetailsDialog.getTxtBxOthers().setText(null);
		followUpDetailsDialog.getTxtBxMonthlyWt().setText(null);
		//Labels to default Black color
		clearGUILabelColors(blackColor);


	}

	@Override
	public void setGuiDetailsToDto(Object _guiObject) {
		final EditUpdateFollowUpDetailsDialog _editFollowUpDialog = (EditUpdateFollowUpDetailsDialog)_guiObject;
		followUpDetailsDto = new FollowUpDetailsDto();
		if(_editFollowUpDialog.getDtChoDateOfDeworming().getDate() != null){
			followUpDetailsDto.setDeWormingDate(_editFollowUpDialog.getDtChoDateOfDeworming().getDate());
		}
		if(_editFollowUpDialog.getDtChoDateOfDeticking().getDate() != null){
			followUpDetailsDto.setDeTickingDate(_editFollowUpDialog.getDtChoDateOfDeticking().getDate());
		}
		if(_editFollowUpDialog.getDtChoDateOfVaccination().getDate() != null){
			followUpDetailsDto.setVaccinationDate(_editFollowUpDialog.getDtChoDateOfVaccination().getDate());
		}
		followUpDetailsDto.setOther(_editFollowUpDialog.getTxtBxOthers().getText());
		followUpDetailsDto.setNutrition(_editFollowUpDialog.getTxtBxNutrition().getText());
		followUpDetailsDto.setBirthWeight(_editFollowUpDialog.getTxtBxBirthWeight().getText());
		followUpDetailsDto.setMonthlyWeight(_editFollowUpDialog.getTxtBxMonthlyWt().getText());
		followUpDetailsDto.setFollowUpId(_followUpId);
		validate(followUpDetailsDto);
		//EditProgencyDialog.tempFollowUpLst.add(followUpDetailsDto);
		//followUpLst.add(addFollowUpDetailsDto);
		//tempFollowUpLst.add(addFollowUpDetailsDto);
		/*followUpLst.add(followUpDetailsDto);
		totalLst = _editFollowUpDialog.getTmpFollowUpLst();

		totalLst.addAll(followUpLst);

		_editFollowUpDialog.setTmpFollowUpLst(totalLst);

		 */		//ProgencyDto.setFollowUpDetails(tempFollowUpLst);
	}

	private void validate(FollowUpDetailsDto addFollowUpDetailsDto2) {

		try{
			String dewromingDateErr = ValidatorUtils.validateString(rb.getString("dialog_label_prgy_dworing"), DU.format(addFollowUpDetailsDto2.getDeWormingDate(), DBConstants.DISPLAY_DATE_FORMAT), 6, 50, true);
			if(dewromingDateErr != null)
				errorMsgs.put(rb.getString("dialog_label_prgy_dworing").toUpperCase(), dewromingDateErr);
			String detickinggDateErr = ValidatorUtils.validateString(rb.getString("dialog_label_prgy_deticking"), DU.format(addFollowUpDetailsDto2.getDeWormingDate(), DBConstants.DISPLAY_DATE_FORMAT), 6, 50, true);
			if(detickinggDateErr != null)
				errorMsgs.put(rb.getString("dialog_label_prgy_deticking").toUpperCase(), detickinggDateErr);

			String vaccinationDateErr = ValidatorUtils.validateString(rb.getString("dialog_label_prgy_vaccintion"), DU.format(addFollowUpDetailsDto2.getDeWormingDate(), DBConstants.DISPLAY_DATE_FORMAT), 6, 50, true);
			if(vaccinationDateErr != null)
				errorMsgs.put(rb.getString("dialog_label_prgy_vaccintion").toUpperCase(), vaccinationDateErr);
			String monthWeightErr = ValidatorUtils.validateDecimal(rb.getString("dialog_label_prgy_grwmnthwgt"), addFollowUpDetailsDto2.getMonthlyWeight(), ValidatorUtils.DECIMALS, 0, Double.MAX_VALUE, 0, 2, false, false);
			if(monthWeightErr != null)
				errorMsgs.put(rb.getString("dialog_label_prgy_grwmnthwgt").toUpperCase(), monthWeightErr);
			String birthWeightErr = ValidatorUtils.validateDecimal(rb.getString("dialog_label_prgy_grwbrthwgt"), addFollowUpDetailsDto2.getBirthWeight(), ValidatorUtils.DECIMALS, 0, Double.MAX_VALUE, 0, 2, false, false);
			if(birthWeightErr != null)
				errorMsgs.put(rb.getString("dialog_label_prgy_grwbrthwgt").toUpperCase(), birthWeightErr);

			if(errorMsgs != null && !errorMsgs.isEmpty()){
				throw new Exception();
			}
		}catch (Exception e) {
			String errMsg = null;
			List<String> errorMsgLst = new ArrayList<String>();
			if(errorMsgs.containsKey(rb.getString("dialog_label_prgy_dworing").toUpperCase())){
				followUpDetailsDialog.getLblDeworing().setForeground(redColor);
				errMsg = errorMsgs.remove(rb.getString("dialog_label_prgy_dworing").toUpperCase());
				errorMsgLst.add(errMsg);
			}
			if(errorMsgs.containsKey(rb.getString("dialog_label_prgy_vaccintion").toUpperCase())){
				followUpDetailsDialog.getLblVaccinations().setForeground(redColor);
				errMsg = errorMsgs.remove(rb.getString("dialog_label_prgy_vaccintion").toUpperCase());
				errorMsgLst.add(errMsg);
			}
			if(errorMsgs.containsKey(rb.getString("dialog_label_prgy_deticking").toUpperCase())){
				followUpDetailsDialog.getLblDeticking().setForeground(redColor);
				errMsg = errorMsgs.remove(rb.getString("dialog_label_prgy_deticking").toUpperCase());
				errorMsgLst.add(errMsg);
			}
			if(errorMsgs.containsKey(rb.getString("dialog_label_prgy_grwbrthwgt").toUpperCase())){
				followUpDetailsDialog.getLblBirthWt().setForeground(redColor);
				errMsg = errorMsgs.remove(rb.getString("dialog_label_prgy_grwbrthwgt").toUpperCase());
				errorMsgLst.add(errMsg);
			}
			if(errorMsgs.containsKey(rb.getString("dialog_label_prgy_grwmnthwgt").toUpperCase())){
				followUpDetailsDialog.getLblMonthlyWt().setForeground(redColor);
				errMsg = errorMsgs.remove(rb.getString("dialog_label_prgy_grwmnthwgt").toUpperCase());
				errorMsgLst.add(errMsg);
			}
			String errorMessages = "";
			Iterator<String> itr = errorMsgLst.iterator();
			while(itr.hasNext()){
				errMsg = itr.next();
				errorMessages += errMsg;

				if(!errMsg.contains("\n")){
					errorMessages += "\n";
				}
			}
			JOptionPane.showMessageDialog(followUpDetailsDialog, errorMessages);
			if(!errorMessages.equals("")){
				errorsFound   = true;
			}
		}
	}


	@Override
	public void setDtoDetailsToGui(Object _dtoObject) {
		// TODO Auto-generated method stub

	}


	@Override
	public void clearGUILabelColors(Color color) {
		followUpDetailsDialog.getLblBirthWt().setForeground(color);
		followUpDetailsDialog.getLblDeticking().setForeground(color);
		followUpDetailsDialog.getLblDeworing().setForeground(color);
		followUpDetailsDialog.getLblMonthlyWt().setForeground(color);
		followUpDetailsDialog.getLblNutrition().setForeground(color);
		followUpDetailsDialog.getLblOthers().setForeground(color);
		followUpDetailsDialog.getLblVaccinations().setForeground(color);

	}


}
